package com.dfd.mango.admin.controller;

import com.dfd.mango.admin.entity.SysUser;
import com.dfd.mango.admin.security.JwtAuthenticationToken;
import com.dfd.mango.admin.service.SysCaptchaService;
import com.dfd.mango.admin.service.SysUserService;
import com.dfd.mango.admin.util.PasswordUtils;
import com.dfd.mango.admin.util.SecurityUtils;
import com.dfd.mango.admin.vo.LoginBean;
import com.dfd.mango.common.IOUtils;
import com.dfd.mango.core.http.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 登录控制器
 *
 */
@RestController
public class SysLoginController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Autowired
    private SysUserService userService;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private SysCaptchaService sysCaptchaService;

    /**
     * 验证码
     */
    @GetMapping("captcha.jpg")
    public void captcha(HttpServletResponse response, String uuid) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //获取图片验证码
        BufferedImage image = sysCaptchaService.getCaptcha(uuid);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    /**
     * 登录
     * @param loginBean 登录对象
     * @param request
     * @return
     */
    @PostMapping("login")
    public HttpResult login(@RequestBody LoginBean loginBean, HttpServletRequest request) {
        String username = loginBean.getAccount();
        String password = loginBean.getPassword();
        String captcha = loginBean.getCaptcha();
        if (captcha == null) {
            return HttpResult.error("验证码为空！");
        }
        boolean b = sysCaptchaService.validate(loginBean.getUuid(), captcha);
        if (!b) {
            return HttpResult.error("验证码不正确");
        }

        SysUser user = userService.findByName(username);
        if (user == null) {
            return HttpResult.error("用户不存在！");
        }
        if (!PasswordUtils.matches(user.getSalt(), password, user.getPassword())) {
            return HttpResult.error("账号或者密码错误！");
        }
        if (user.getStatus() == 0) {
            return HttpResult.error("账号被锁定，请联系管理员！");
        }

        // 系统登录认证
        JwtAuthenticationToken token = SecurityUtils.login(request, username, password, authenticationManager);
        return HttpResult.ok(token);
    }
}
